<template>
  <div class="app-container">
    <div class="filter-container">
      <center>
        <el-form>
          <el-form-item>
            <el-input v-model="search" placeholder="请输入搜索设备内容" style='width: 50%;'></el-input>
            <el-button type="primary" icon="el-icon-search" circle style='margin-left:10px;' @click="lists()"></el-button>
          </el-form-item>
        </el-form>
      </center>
    </div>
<!-- 这是一个 表格 -->
    <el-table
      :data="lists" v-loading.body="listLoading" element-loading-text="拼命加载中" border fit highlight-current-row
      style="width: 97.5%;margin:20px;margin-left:20px;">
    <el-table-column align="center" prop="applyId" label="审批序号">
    </el-table-column>
    <el-table-column align="center" prop="userName" label="申请用户">
    </el-table-column>
    <el-table-column align="center" prop="apply_device_id" label="设备编号">
    </el-table-column>
    <el-table-column align="center" prop="devModel" label="设备型号">
    </el-table-column>
    <el-table-column align="center" prop="devName" label="设备名称">
    </el-table-column>
    <el-table-column align="center" prop="devPosition" label="使用位置">
    </el-table-column>
    <el-table-column align="center" prop="function" label="使用功能">
    </el-table-column>
    <el-table-column align="center" prop="createTime" label="创建时间" width="190">
    </el-table-column>
    <el-table-column align="center" prop="sTime" label=" 开始时间" width="190">
    </el-table-column>
    <el-table-column align="center" prop="eTime" label=" 结束时间" width="190">
    </el-table-column>
    <el-table-column align="center" prop="isAnxious" label="是否加急">
    </el-table-column>
    <el-table-column align="center" label="操作">
      <template slot-scope="scope">
        <el-button align="center" type="primary" size="small" v-if="hasPerm('devices:update')" @click="showUpdate(scope.$index)" style="width:70px">
          审批</el-button>
      </template>
    </el-table-column>
    </el-table>

<!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-size="listQuery.pageRow"
      layout="sizes, prev, pager, next, jumper"
      :total="totalCount"
      :page-sizes="[10, 20, 50, 100]">
    </el-pagination>

    <!-- 这是一个button 点击弹框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form class="small-space" :model="tempDevice" label-position="left" label-width="100px"
               style='width: 600px; margin-left:50px;'>
        <el-form-item label="审批序号" required>
          <el-input type="text" v-model="tempDevice.applyId" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="申请用户" required>
          <el-input type="text" v-model="tempDevice.userName" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="设备编号" required>
          <el-input type="text" v-model="tempDevice.apply_device_id" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="设备型号" required>
          <el-input type="text" v-model="tempDevice.devModel" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="设备名称" required>
          <el-input type="text" v-model="tempDevice.devName" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="使用位置" required>
          <el-input type="text" v-model="tempDevice.devPosition" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="使用功能" required>
          <el-input type="text" v-model="tempDevice.function" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="创建时间" required>
          <el-input type="text" v-model="tempDevice.createTime" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="开始时间" required>
          <el-input type="text" v-model="tempDevice.sTime" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="结束时间" required>
          <el-input type="text" v-model="tempDevice.eTime" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="是否加急" required>
          <el-input type="text" v-model="tempDevice.isAnxious" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="审批情况" required>
          <el-radio align="center" v-model="tempDevice.situation" label="通过" style="width:100px" border>通过</el-radio>
          <el-radio align="center" v-model="tempDevice.situation" label="不通过" style="width:100px" border>不通过</el-radio>
          <p style="color:#848484;">说明:红色权限为对应菜单内的必选权限</p>
        </el-form-item> 
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateApply">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {mapGetters} from 'vuex'

  export default {
    data() {
      return {
        search:'',
        list: [],//表格的数据
        totalCount: 0,
        listLoading: false,//数据加载等待动画
        dialogStatus: 'update',
        dialogFormVisible: false,
        textMap: {
          update: '编辑'
        },
        listQuery: {
          pageNum: 1, //页码
          pageRow: 50, //每页条数
        },
        tempDevice: {
          applyId: '',
          userName: '',
          apply_device_id: '',
          devModel: '',
          devName: '',
          devPosition: '',
          function: '',
          createTime: '',
          sTime: '',
          eTime: '',
          isAnxious: '',
          situation: '',
          permissions: [],
        },
      }
    },
    created() {
      this.getList();
    },
    computed: {
      // 模糊搜索
      lists () {
        const search = this.search
        if (search) {
          // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
          // 注意： filter() 不会对空数组进行检测。
          // 注意： filter() 不会改变原始数组。
          return this.list.filter(data => {
            // some() 方法用于检测数组中的元素是否满足指定条件;
            // some() 方法会依次执行数组的每个元素：
            // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
            // 如果没有满足条件的元素，则返回false。
            // 注意： some() 不会对空数组进行检测。
            // 注意： some() 不会改变原始数组。
            return Object.keys(data).some(key => {
              // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
              // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
              return String(data[key]).toLowerCase().indexOf(search) > -1
            })
          })
        }
        return this.list
      }
    },
    methods: {
      getList() {
        //查询列表
        this.listLoading = true;
        this.api({
          url: "/devices/listApply",
          method: "get",
          params: this.listQuery
        }).then(data => {
          console.log(data);
          this.listLoading = false;
          this.list = data.list;
          this.totalCount = data.totalCount;
        })
      },
      handleSizeChange(val) {
        //改变每页数量
        this.listQuery.pageRow = val
        this.handleFilter();
      },
      handleCurrentChange(val) {
        //改变页码
        this.listQuery.pageNum = val
        this.getList();
      },
      handleFilter() {
        //查询事件
        this.listQuery.pageNum = 1
        this.getList()
      },
      getIndex($index) {
        //表格序号
        return (this.listQuery.pageNum - 1) * this.listQuery.pageRow + $index + 1
      },
      showUpdate($index) {  
        let device = this.list[$index];
        this.tempDevice.applyId = device.applyId;
        this.tempDevice.userName = device.userName;
        this.tempDevice.apply_device_id = device.apply_device_id;
        this.tempDevice.devModel = device.devModel;
        this.tempDevice.devName = device.devName;
        this.tempDevice.function = device.function;
        this.tempDevice.devPosition = device.devPosition;
        this.tempDevice.createTime = device.createTime;
        this.tempDevice.eTime = device.eTime;
        this.tempDevice.sTime = device.sTime;
        this.tempDevice.isAnxious = device.isAnxious;
        this.dialogStatus = "update"
        this.dialogFormVisible = true
      },
      updateApply() {
        //修改角色
        this.api({
          url: "/devices/updateApply",
          method: "post",
          data: this.tempDevice
        }).then(() => {
          this.getList();
          this.dialogFormVisible = false
        })
      },
    }
  }
</script>
<style scoped>
  .requiredPerm {
    color: #ff0e13;
  }
</style>
